package com.example.module_car.ui

import androidx.lifecycle.MutableLiveData
import com.example.lib_common.base.BaseViewModel
import com.example.lib_common.base.state.Failed
import com.example.lib_common.base.state.Loading
import com.example.lib_common.base.state.RequestEvent
import com.example.lib_common.base.state.Success
import com.example.module_car.model.CarEntityItem
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

@HiltViewModel
class CarViewModel @Inject constructor(private val repo : CarRepository) : BaseViewModel() {
    private val mCarState_ = MutableStateFlow<RequestEvent>(Loading)
    val mCarState = mCarState_.asStateFlow()
     val mCarList = MutableLiveData<CarEntityItem>()

    fun getQueryCarList(id : Int){
        execute(
            action = repo.getQueryCarList(id),
            onError = {
                mCarState_.value = Failed("数据获取失败"+it)
            },
            onFinish = {
                mCarState_.value = Success
                mCarList.value = it.data
            }
        )
    }

}